﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Weibo4Sharp.Model;
using Hammock.Web;

namespace Weibo4Sharp.Service
{
    public partial class WeiboService
    {
        /// <summary>
        /// List count of a weibo 
        /// </summary>
        /// <param name="id">weibo id</param>
        /// <returns></returns>
        public virtual IEnumerable<WeiboCount> ListWeiboCounts(long id)
        {
            return WithHammock<IEnumerable<WeiboCount>>(
               "statuses/counts", FormatAsString,
               "?ids=", id);
        }
        

        /// <summary>
        /// List count of weibos
        /// </summary>
        /// <param name="ids">weiboids, like: {123, 124, 138}</param>
        /// <returns></returns>
        public virtual IEnumerable<WeiboCount> ListWeiboCounts(IEnumerable<object> ids)
        {
            return WithHammock<IEnumerable<WeiboCount>>(
               "statuses/counts", FormatAsString,
               "?ids=", ids);
        }

        /// <summary>
        /// Bind weibo with counts, Not an offical API
        /// </summary>        
        /// <param name="weibos"></param>
        /// <returns></returns>
        public virtual IEnumerable<WeiboStatus> ListWeiboCountsBindToWeibos(IEnumerable<WeiboStatus> weibos)
        {
            var ids = new HashSet<object>();

            foreach (var weibo in weibos)
            {                
                ids.Add(weibo.Id);
                if (weibo.RetweetedStatus != null && weibo.RetweetedStatus.Id > 0)
                {
                    ids.Add(weibo.RetweetedStatus.Id);
                }
            }

            var counts = WithHammock<IEnumerable<WeiboCount>>(
               "statuses/counts", FormatAsString,
               "?ids=", ids);

            foreach (var weibo in weibos)
            {
                var count = counts.FirstOrDefault(c => c.Id == weibo.Id);
                if (count == null) continue;
                weibo.CommentCount = count.Comments;
                weibo.RepostCount = count.Rt;

                if (weibo.RetweetedStatus != null)
                {
                    count = counts.FirstOrDefault(c => c.Id == weibo.RetweetedStatus.Id);
                    if (count != null)
                    {
                        weibo.RetweetedStatus.CommentCount = count.Comments;
                        weibo.RetweetedStatus.RepostCount = count.Rt;
                    }
                }
            }

            return weibos;
        }
       
        public virtual IEnumerable<WeiboComment> ListWeiboComments(int count = 20, int page = 1)
        {
            return WithHammock<IEnumerable<WeiboComment>>(
                "statuses/comments_timeline", FormatAsString,
                "?count=", count,
                "&page=", page
                );
        }

        public virtual IEnumerable<WeiboComment> ListWeiboCommentsByMe(int count = 20, int page = 1)
        {
            return WithHammock<IEnumerable<WeiboComment>>(
                "statuses/comments_by_me", FormatAsString,
                "?count=", count,
                "&page=", page
                );
        }

        public virtual IEnumerable<WeiboComment> ListWeiboCommentsToMe(int count = 20, int page = 1)
        {
            return WithHammock<IEnumerable<WeiboComment>>(
                "statuses/comments_to_me", FormatAsString,
                "?count=", count,
                "&page=", page
                );
        }

        public virtual WeiboUnread GetWeiboUnread()
        {
            return WithHammock<WeiboUnread>(
                "statuses/unread", FormatAsString);
        }

        public virtual WeiboUnread GetWeiboUnreadSince(long sinceId)
        {
            return WithHammock<WeiboUnread>(
                "statuses/unread", FormatAsString,
                "?with_new_status=", 1,
                "@since_id=", sinceId
                );
        }

        /// <summary>
        /// Reset unread count
        /// </summary>
        /// <param name="type">1=comment|2=mention|3=dm|4=follow</param>
        /// <returns></returns>
        public virtual WeiboResult ResetCount(int type)
        {
            return WithHammock<WeiboResult>(
                WebMethod.Post,
                "statuses/reset_count", FormatAsString,
                "?type=", type
            );
        }

        public virtual IEnumerable<WeiboEmotion> ListEmotions()
        {
            return WithHammock<IEnumerable<WeiboEmotion>>(
                "emotions", FormatAsString,
                "?source=", _consumerKey
            );
        }

        public virtual IEnumerable<WeiboEmotion> ListEmotions(string type = "face", string language = "cnname")
        {
            return WithHammock<IEnumerable<WeiboEmotion>>(
                "emotions", FormatAsString,
                "?source=", _consumerKey,
                "&type=", type,
                "&language=", language
            );
        }


        public virtual IEnumerable<WeiboStatus> ListReposts(long id, int count = 20, int page = 1)
        {
            return WithHammock<IEnumerable<WeiboStatus>>(
                "statuses/repost_timeline", FormatAsString,
                "?id=", id,
                "&count=", count,
                "&page=", page);
        }

        public virtual IEnumerable<WeiboStatus> ListRepostsSince(long id, long sinceId, int count = 20, int page = 1)
        {
            return WithHammock<IEnumerable<WeiboStatus>>(
               "statuses/repost_timeline", FormatAsString,
               "?id=", id,
               "&since_id=", sinceId,
               "&count=", count,
               "&page=", page);
        }

        public virtual IEnumerable<WeiboStatus> ListRepostsBefor(long id, long maxId, int count = 20, int page = 1)
        {
            return WithHammock<IEnumerable<WeiboStatus>>(
               "statuses/repost_timeline", FormatAsString,
               "?id=", id,
               "&max_id=", maxId,
               "&count=", count,
               "&page=", page);
        }

        public virtual IEnumerable<WeiboStatus> ListRepostsBetween(long id, long sinceId, long maxId, int count = 20, int page = 1)
        {
            return WithHammock<IEnumerable<WeiboStatus>>(
               "statuses/repost_timeline", FormatAsString,
               "?id=", id,
               "&since_id=", sinceId,
               "&max_id=", maxId,
               "&count=", count,
               "&page=", page);
        }


        public virtual IEnumerable<WeiboStatus> ListRepostsByMe(long id, int count = 20, int page = 1)
        {
            return WithHammock<IEnumerable<WeiboStatus>>(
                "statuses/repost_by_me", FormatAsString,
                "?id=", id,
                "&count=", count,
                "&page=", page);
        }

        public virtual IEnumerable<WeiboStatus> ListRepostsByMeSince(long id, long sinceId, int count = 20, int page = 1)
        {
            return WithHammock<IEnumerable<WeiboStatus>>(
               "statuses/repost_by_me", FormatAsString,
               "?id=", id,
               "&since_id=", sinceId,
               "&count=", count,
               "&page=", page);
        }

        public virtual IEnumerable<WeiboStatus> ListRepostsByMeBefor(long id, long maxId, int count = 20, int page = 1)
        {
            return WithHammock<IEnumerable<WeiboStatus>>(
               "statuses/repost_by_me", FormatAsString,
               "?id=", id,
               "&max_id=", maxId,
               "&count=", count,
               "&page=", page);
        }

        public virtual IEnumerable<WeiboStatus> ListRepostsByMeBetween(long id, long sinceId, long maxId, int count = 20, int page = 1)
        {
            return WithHammock<IEnumerable<WeiboStatus>>(
               "statuses/repost_by_me", FormatAsString,
               "?id=", id,
               "&since_id=", sinceId,
               "&max_id=", maxId,
               "&count=", count,
               "&page=", page);
        }
    }
}
